package top.yzhao.trackdatasystem.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yzhao.trackdatasystem.dao.TrackDataDao;
import top.yzhao.trackdatasystem.form.TrackDataForm;
import top.yzhao.trackdatasystem.model.TrackData;
import top.yzhao.trackdatasystem.service.TrackDataService;
import top.yzhao.trackdatasystem.utils.FileOpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * User: zhaoyang
 * Date: 2020/3/13
 * Time: 2:22
 * Description: No Description
 */
@Service
public class TrackDataServiceImpl implements TrackDataService {

    @Autowired
    private TrackDataDao trackDataDao;

    @Override
    public List<String[]> getLeastTrackData() {
        List<String[]> rs = new ArrayList<>();

        List<TrackData> tdList = trackDataDao.getLeastTrackData();
        int size = tdList.size();
        String[] Lprfarrs = new String[size];
        String[] Rprfarrs = new String[size];
        String[] Lalnarrs = new String[size];
        String[] Ralnarrs = new String[size];
        String[] Gagearrs = new String[size];
        String[] Cantarrs = new String[size];
        String[] Xlvlarrs = new String[size];
        String[] Wrap_1arrs = new String[size];
        String[] Cvtrarrs = new String[size];
        String[] Lacc_1arrs = new String[size];
        String[] Vaccarrs = new String[size];
        String[] LprfLW_3arrs = new String[size];
        String[] RprfLW_3arrs = new String[size];
        String[] LalnLW_3arrs = new String[size];
        String[] RalnLW_3arrs = new String[size];

        for (int i = 0; i < size; i++) {
            Lprfarrs[i] = tdList.get(i).getLprf();
            Rprfarrs[i] = tdList.get(i).getRprf();
            Lalnarrs[i] = tdList.get(i).getLaln();
            Ralnarrs[i] = tdList.get(i).getRaln();
            Gagearrs[i] = tdList.get(i).getGage();
            Cantarrs[i] = tdList.get(i).getCant();
            Xlvlarrs[i] = tdList.get(i).getXlvl();
            Wrap_1arrs[i] = tdList.get(i).getWrap_1();
            Cvtrarrs[i] = tdList.get(i).getCvtr();
            Lacc_1arrs[i] = tdList.get(i).getLacc_1();
            Vaccarrs[i] = tdList.get(i).getVacc();
            LprfLW_3arrs[i] = tdList.get(i).getLprfLW_3();
            RprfLW_3arrs[i] = tdList.get(i).getRprfLW_3();
            LalnLW_3arrs[i] = tdList.get(i).getLalnLW_3();
            RalnLW_3arrs[i] = tdList.get(i).getRalnLW_3();
        }

        rs.add(Lprfarrs);
        rs.add(Rprfarrs);
        rs.add(Lalnarrs);
        rs.add(Ralnarrs);
        rs.add(Gagearrs);
        rs.add(Cantarrs);
        rs.add(Xlvlarrs);
        rs.add(Wrap_1arrs);
        rs.add(Cvtrarrs);
        rs.add(Lacc_1arrs);
        rs.add(Vaccarrs);
        rs.add(LprfLW_3arrs);
        rs.add(RprfLW_3arrs);
        rs.add(LalnLW_3arrs);
        rs.add(RalnLW_3arrs);

        return rs;
    }

    @Override
    public int getTotalLeastTrackData() {
        return trackDataDao.getTotalLeastTrackData();
    }

    @Override
    public List<TrackData> getTrackData(TrackDataForm trackDataForm) {
        return trackDataDao.getTrackData(trackDataForm);
    }

    @Override
    public int getTotalTrackData(TrackDataForm trackDataForm) {
        return trackDataDao.getTotalTrackData(trackDataForm);
    }

    @Override
    public String[] getTrackDataLine(TrackDataForm trackDataForm) {
        List<TrackData> rs = trackDataDao.getTrackDataLine(trackDataForm);
        int size = rs.size();
        String [] td = new String[size];
        String sensor_name = trackDataForm.getSensorname();
        if (trackDataForm!=null){
            for (int i = 0; i < size; i++) {
                switch (sensor_name){
                    case "左高低":
                        td[i] = rs.get(i).getLprf();
                        break;
                    case "右高低":
                        td[i] = rs.get(i).getRprf();
                        break;
                    case "左轨向":
                        td[i] = rs.get(i).getLaln();
                        break;
                    case "右轨向":
                        td[i] = rs.get(i).getRaln();
                        break;
                    case "轨距":
                        td[i] = rs.get(i).getGage();
                        break;
                    case "超高":
                        td[i] = rs.get(i).getCant();
                        break;
                    case "水平":
                        td[i] = rs.get(i).getXlvl();
                        break;
                    case "三角坑":
                        td[i] = rs.get(i).getWrap_1();
                        break;
                    case "曲率":
                        td[i] = rs.get(i).getCvtr();
                        break;
                    case "车体横向加速度":
                        td[i] = rs.get(i).getLacc_1();
                        break;
                    case "车体垂向加速度":
                        td[i] = rs.get(i).getVacc();
                        break;
                    case "左高低长波":
                        td[i] = rs.get(i).getLprfLW_3();
                        break;
                    case "右高低长波":
                        td[i] = rs.get(i).getRprfLW_3();
                        break;
                    case "左轨向长波":
                        td[i] = rs.get(i).getLalnLW_3();
                        break;
                    case "右轨向长波":
                        td[i] = rs.get(i).getRalnLW_3();
                        break;
                }
            }
        }


        return td;
    }

    /**
     * 演示代码
     * 暂时从本地excle读取数据
     * @param trackDataForm
     * @return
     */
    @Override
    public String[] getTrackDataLineFExcel(TrackDataForm trackDataForm, int od) {
        String path = "";
        if(od == 1){
            path = "/static/tmp_wmData/base.txt";
        }
        if(od == 2){
            path = "/static/tmp_wmData/origin_tobe_recorrected.txt";
        }
        if(od == 3){
            path = "/static/tmp_wmData/recorrected.txt";
        }
        List<TrackData> rs = FileOpUtils.readTxt(path);


        int size = 19560; // 准备写入确定的值
        int start = 19210;
        String [] td = new String[size - start];
        String sensor_name = trackDataForm.getSensorname();

        int j = 0;
        if (trackDataForm!=null){
            for (int i = start; i < size; i++) { // 19000 20000
                switch (sensor_name){
                    case "左高低":
                        td[j] = rs.get(i).getLprf();
                        break;
                    case "右高低":
                        td[j] = rs.get(i).getRprf();
                        break;
                    case "左轨向":
                        td[j] = rs.get(i).getLaln();
                        break;
                    case "右轨向":
                        td[j] = rs.get(i).getRaln();
                        break;
                    case "轨距":
                        td[j] = rs.get(i).getGage();
                        break;
                    case "超高":
                        td[j] = rs.get(i).getCant();
                        break;
                    case "水平":
                        td[j] = rs.get(i).getXlvl();
                        break;
                    case "三角坑":
                        td[j] = rs.get(i).getWrap_1();
                        break;
                    case "曲率":
                        td[j] = rs.get(i).getCvtr();
                        break;
                    case "车体横向加速度":
                        td[j] = rs.get(i).getLacc_1();
                        break;
                    case "车体垂向加速度":
                        td[j] = rs.get(i).getVacc();
                        break;
                    case "左高低长波":
                        td[j] = rs.get(i).getLprfLW_3();
                        break;
                    case "右高低长波":
                        td[j] = rs.get(i).getRprfLW_3();
                        break;
                    case "左轨向长波":
                        td[j] = rs.get(i).getLalnLW_3();
                        break;
                    case "右轨向长波":
                        td[j] = rs.get(i).getRalnLW_3();
                        break;
                }
                j++;
            }
        }
        return td;
    }
}
